/**
 * Created by shenhai on 15/10/15.
 */

angular.module('mis.leifeng.controllers', [])
    .controller('leifengApplyCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/appUser/roleApplyList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.applyList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.applyList = null;
            },
            params: {type:2}
        });

        //搜索
        $scope.search = function (btn) {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            //var community = $scope.cascading.getVillageId(btn);

            var body = {type:2};
            //if (community) body.community = community;
            if ($scope.status) body.status = $scope.status;
            if ($scope.userId) body.user = $scope.userId;
            if ($scope.name) body.name = $scope.name;
            if ($scope.nickName) body.name = $scope.nickName;
            if ($scope.mobilePhone) body.phone = $scope.mobilePhone;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/roleApplyList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.applyList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;
            }).error(function (err) {
                $scope.applyList = null;
                alert(err);
            });

        };

        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };


        $scope.declineShow = function(apply){
            $scope.apply = apply;
            $scope.apply.msg = "非常感谢对于雷锋团的支持，很抱歉没有通过本次雷锋团的申请，有任何问题随时可以联系66哦～";
            $('#doc-modal-reject').modal({target: '#doc-modal-reject', width: 600, height: 350});
        };


        $scope.handleApply = function(status ,apply){

            var body = {
                id : apply.objId,
                applyCommunity :apply.community.objId,
                applyCommunityName :apply.community.communityName,
                user:apply.user,
                status : status,
                type:2
            };
            if (!status) body.msg = apply.msg;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/roleApplyStatus', body,{
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                if (data.result.status == 1) {
                    var pageNumber = $(".am-active").text();//当前页码
                    $('#doc-modal-reject').modal("close");
                    $scope.pagination.selectPage(pageNumber);
                    //if (status) {
                    //    apply.status =  status?1:-1;
                    //}else {
                    //    $('#doc-modal-reject').modal("close");
                    //}

                }else {
                    alert("请求失败！");
                }

            }).error(function (err) {
                alert("请求失败！");
            });

        };

        //级联框
        //var cascadData = {
        //    provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
        //    cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
        //    cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
        //    villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
        //    ajax: $http
        //};
        //$scope.cascading = new gObj.cascading(cascadData);
        //$scope.cascading.paint($(".city-cascading")[0],true);


    }]).controller('leifengExamCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/appUser/roleLeifengList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.userList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.userList = null;
            },
            params: {userRole:3}
        });

        //搜索
        $scope.search = function (temp) {
            var btn = temp.target;

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {userRole:3};
            var community = $scope.cascading.getVillageId(btn);
            if (community) body.community = community;
            if ($scope.nickName) body.nickName = $scope.nickName;
            if ($scope.mobilePhone) body.mobilePhone = $scope.mobilePhone;
            //if ($("#startDate").val()) body.startDate = $("#startDate").val() + ' 00:00:00';
            //if ($("#endDate").val()) body.endDate = $("#endDate").val() + ' 23:59:59';
            //if (body.startDate && body.endDate && body.startDate > body.endDate) {
            //    alert('开始日期不能大于结束日期');
            //    return;
            //}
            if(!$("#cycleDate").val()){
                alert('请选择考核周期');
                return;
            }else{
                body.cycle = $("#cycleDate").val();
            };

            $('#loading').modal({target: '#loading'});

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/roleLeifengList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;

                    $scope.pagination.totalPage = data.data.totalPage;
                    $scope.pagination.pageHeader($scope.pageNumber);
                    $scope.pagination.searchParams = body;
                }else{
                    alert(data.result.message);
                }
                $('#loading').modal("close");

            }).error(function (err) {
                $('#loading').modal("close");
                $scope.userList = null;
                alert(err);
            });

        };

        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };

        $scope.rejectRole = function(user){

            var body = {userId:user.objId ,userRole:3,community:user.community};
            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/appUser/rejectRole', body,{
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                if(data.result.status == 1) {
                    $scope.userList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;

                    $scope.pagination.totalPage = data.data.totalPage;
                    $scope.pagination.pageHeader($scope.pageNumber);
                    $scope.pagination.searchParams = body;
                } else{
                    alert('操作失败！');
                }
            }).error(function (err) {
                $scope.userList = null;
                alert(err);
            });
        };

        //级联框
        var cascadData = {
            provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
            cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
            cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
            villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
            ajax: $http
        };
        $scope.cascading = new gObj.cascading(cascadData);
        $scope.cascading.paint($(".city-cascading")[0],true);

    }]);